package task1and3;

import java.util.Objects;

public class Worker {
    //  1.私有化成员变量（姓名、年龄、薪水）
    private String name;
    private int age;
    private double salary;

    //3.公有化无参与有参构造方法
    public Worker() {
    }
    public Worker(String name, int age, double salary) {
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    //  2.公有化成员变量的get、set方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age < 0 || age > 150) {
            System.out.println("您的年龄不合常理哦！！！");
        } else {
            this.age = age;
        }
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        if(salary < 0.0) {
            System.out.println("您的工资不合理哦！！！");
        } else {
            this.salary = salary;
        }
    }

    //  4.重写toString方法
    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    //  5.重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return age == worker.age &&
                Double.compare(worker.salary, salary) == 0 &&
                Objects.equals(name, worker.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }

    //  6.自定义work成员方法
    public void work() {
        System.out.println(getName() + "正在工作");
    }
}

